﻿using System;
using MaLu.Common;
using MaLu.BusiLogic;
using System.Data;
using System.Text;
using MaLu.Data;
using MaLu.Framework.Common;

namespace MaluJg.web.Three
{
    public partial class Three1Detail : ThreePage
    {
        protected string CanUpload = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.UseTabber = true;
            Title = "创建事项";
            if (!IsPostBack)
                BindData();
        }
        private void BindData()
        {
            int status = (int)MaLu.Common.Const.EventStatus.已提交;
            btnFinish.Visible = false;
            if (EventID != Guid.Empty)
            {

                DataSet ds = BLThree.GetEvent(Provider, EventID);
                if (ds.Tables.Count >= 3)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Title = "编辑事项";
                        DataRow drInfo = ds.Tables[0].Rows[0];
                        status = int.Parse(drInfo["Status"].ToString());
                        tbDept.Text = drInfo["DeptDspName"].ToString();
                        tbReportTime.Text = drInfo["ReportTime"].ToString();
                        tbMainContent.Text = drInfo["MainContent"].ToString();
                        tbSign.Text = drInfo["Signature"].ToString();
                        tbSug.Text = drInfo["Sug"].ToString();
                        tbSugTime.Text = drInfo["SugTime"].ToString();
                        if (IsLeader)
                            Title = "审阅事项";

                    }

                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        foreach (DataRow item in ds.Tables[1].Rows)
                        {
                            tbAttaches.Text +=
                                string.Format("<li id=\"{0}\" ><a target=\"_blank\" href=\"../common/GetAttach.aspx?id={0}\">{1}</a>&nbsp;&nbsp;", item["AttachmentID"].ToString(), item["DisplayFileName"].ToString());

                            if (status == (int)MaLu.Common.Const.EventStatus.已提交 && !IsLeader)
                                tbAttaches.Text += "<span  onclick=\"delAtt(this)\">[删除]</span>";

                            tbAttaches.Text += "</li>";
                        }
                    }

                    DataTable dtIns = ds.Tables[2];
                    string temp = string.Empty;
                    foreach (DataRow item in dtIns.Rows)
                    {

                        temp += ";" + item["Name"].ToString().Replace("|", "@2").Replace("''", "'");


                    }
                    tbInstances.Text = temp.Trim(new char[] { ';' });
                }
            }

            if (IsLeader)
            {
                tbDept.ReadOnly = true;
                tbReportTime.ReadOnly = true;
                tbSign.ReadOnly = true;
                tbMainContent.ReadOnly = true;
                btnStart.Text = "<span><img src=\"../Images/Icon/Object/File-add.gif\" /></span><span>审阅</span>";

            }
            else
            {
                tbSugTime.ReadOnly = true;
                tbSug.ReadOnly = true;

            }
            if (status == (int)MaLu.Common.Const.EventStatus.已审阅)
            {
                CanUpload = "1";
                btnStart.Visible = false;

            }
            else
                btnFinish.Visible = true;
        }
        protected Guid EventID
        {
            get
            {
                Guid gd = Guid.Empty;
                try
                {
                    gd = new Guid(Request["EventID"] ?? Guid.Empty.ToString());

                }
                catch { }

                return gd;

            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int result = 0;
            int status = (int)MaLu.Common.Const.EventStatus.已提交;
            string alert = "已提交！";
            if (IsLeader)
            {

                status = (int)MaLu.Common.Const.EventStatus.已审阅;
                alert = "已审阅！";
            }
            Guid gd = EventID == Guid.Empty ? Guid.NewGuid() : EventID;
            result = BLThree.SaveEvent(Provider, gd, CurrentUser.MainDepartmentID, tbDept.Text, tbReportTime.Text, tbMainContent.Text,
                 status, tbSign.Text, tbSug.Text, tbSugTime.Text, CurrentUser.Name, DateTime.Now, CurrentUser.Name, tbInstances.Text, tbAttachIDs.Text);

            if (result == 1 && !IsLeader)
            {
                alert = "已审阅,不能再提交！";

            }
            else
            {
                StringBuilder sb = new StringBuilder();
                string[] trs = tbInstances.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                sb.AppendLine("delete from TEvent1Instance where EventID='" + gd + "'");
                foreach (string item in trs)
                {
                    
                    sb.AppendFormat(@"INSERT INTO [TEvent1Instance]
           ([EventID]  ,[Name])  VALUES
           ('{0}',{1});", gd,Util.ReturnSqlValue( item.Replace("@1", ";").Replace("@2", "|").Replace("'","''")));
                }
                SqlHelper.Instance.ExecuteNonQuery(Provider, sb.ToString());
            }

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "<script>alert('" + alert + "');location=location;</script>");

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int result = 0;

            string alert = "已删除！";


            result = BLThree.DeleteEvent(Provider, EventID == Guid.Empty ? Guid.NewGuid() : EventID);

            if (result == 1)
            {
                alert = "已审阅,不能删除！";

            }
            if (result == 0)

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "<script>alert('" + alert + "');window.parent.location.href=window.parent.location.href;</script>");
            if (result == 1)

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "save", "<script>alert('" + alert + "');location=location;</script>");

        }
    }
}